// packageE/simplegraph/simplegraph.js
const app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    device_no:{//生态圈饮水机-设备号
      type:null
    }
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage:  `${app.globalData.imgPath}image.png`,
    clientWidth: "375",
    showMargin: true
  },
  observers: {
    'datas': function (info) {
      this.setData({
        list: this.data.datas.list,
        clientWidth: wx.getSystemInfoSync().windowWidth,
      });
    }
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let res = wx.getSystemInfoSync();
      if (res.platform == 'ios') {
        if(res.model == 'iPhone 13 Pro Max<iPhone14,3>'){
          this.setData({
            showMargin:true
          })
          return
        }
        this.setData({
          showMargin: false
        });
      }
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      list: this.data.datas.list,
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    imgload(evt) {
      // console.log("加载完成",evt)
      this.triggerEvent("imgload", evt);
    },
    toAreaUrl(e) {
      let item = e.currentTarget.dataset.item;
      if (this.data.datas.choose_style == 2) {
        return;
      }
      if (item.is_minApp == 2) {
        if (item.gh_id) {
          if (item.minApp_link) {
            try {
              wx.navigateToMiniProgram({
                appId: item.gh_id,
                path: item.minApp_link,
                extraData: {},
                envVersion: "release",
                success(res) {
                  // 打开成功
                },
                fail(res) {
                  wx.showToast({
                    title: "小程序跳转失败",
                    icon: "none",
                    duration: 2000,
                  });
                },
              });
            } catch (e) {
              wx.showToast({
                title: "小程序跳转失败",
                icon: "none",
                duration: 2000,
              });
            }
          } else {
            wx.showToast({
              title: "请设置跳转小程序后的跳转链接",
              icon: "none",
              duration: 2000,
            });
          }
        } else {
          wx.showToast({
            title: "请设置小程序所需跳转appId",
            icon: "none",
            duration: 2000,
          });
        }
      } else {
        let url = item.h5_link;
	      let value = wx.getStorageSync("yz_uid");
	      let customParams = { mid: value };
        if(this.data.device_no){
          url+='&device_no='+this.data.device_no;
        }
        try {
	        if(url.indexOf("pages/live-player-plugin") > 0) {
		        url = url + `&open_share_ticket=1&custom_params=${encodeURIComponent(JSON.stringify(customParams))}`;
	        }
          wx.navigateTo({
            url: url,
          });
        } catch (e) {
          wx.showToast({
            title: "链接设置错误",
            icon: "none",
            duration: 2000,
          });
        }
      }
    },
    isMemberLevelUrl(item){
      if(!item.memberLevelOptions) return item;//没有开启会员等级跳转设置
      return new Promise((resolve, reject) => {
        let urlStr = app.getNetAddresss('plugin.decorate.frotend.basic-component.simplegraph');
        app._postNetWork({
          url: urlStr,
          data: {
            simplegraph_id: this.data.component_id,
            decorate_id: this.data.page_id,
            remote_data_list_id: item.id
          },
          success: (resdata) => {
            let res = resdata.data;
            if (res.result !== 1){
              app.tips(res.msg)
              return resolve(item);
            } 
            //请求接口返回数据，防止会员升级后，装修的单图会员等级数据没变化
            resolve(res.data.jump_link);
          },
          fail: function (res) {
            reject(item);
          },
        });
      });
    },
    async goimgurl(e) {
      let data = e.currentTarget.dataset.item;
      let item = await this.isMemberLevelUrl(data);
      if (this.data.datas.choose_style != 2) {
        return;
      }
      if (item.is_minApp == 2) {
        if (item.gh_id) {
          if (item.minApp_link) {
            try {
              wx.navigateToMiniProgram({
                appId: item.gh_id,
                path: item.minApp_link,
                extraData: {},
                envVersion: "release",
                success(res) {
                  // 打开成功
                },
                fail(res) {
                  wx.showToast({
                    title: "小程序跳转失败",
                    icon: "none",
                    duration: 2000,
                  });
                },
              });
            } catch (e) {
              wx.showToast({
                title: "小程序跳转失败",
                icon: "none",
                duration: 2000,
              });
            }
          } else {
            if (app._isTextEmpty(item.minApp_link)) {
              wx.previewImage({
                current: item.image, // 当前显示图片的http链接
                urls: [item.image] // 需要预览的图片http链接列表
              });
              return;
            }
            wx.showToast({
              title: "请设置跳转小程序后的跳转链接",
              icon: "none",
              duration: 2000,
            });
          }
        } else {
          if (app._isTextEmpty(item.gh_id)) {
            wx.previewImage({
              current: item.image, // 当前显示图片的http链接
              urls: [item.image] // 需要预览的图片http链接列表
            });
            return;
          }
          wx.showToast({
            title: "请设置小程序所需跳转appId",
            icon: "none",
            duration: 2000,
          });
        }
      }else if (item.is_minApp == 8) {
        if(item.channels == '1') {
          wx.openChannelsUserProfile({
            finderUserName: item.finderUserName,
          });
        } else if(item.channels == '2') {
          wx.openChannelsLive({
            finderUserName: item.finderUserName,
          });
        } else if(item.channels == '3') {
          wx.openChannelsActivity({
            finderUserName: item.finderUserName,
            feedId: item.feedId
          });
        }
       
      } else {
        let url = item.h5_link;
        let value = wx.getStorageSync("yz_uid");
	      let customParams = { mid: value };
        if( app._isTextEmpty(item.h5_link) ){
          wx.previewImage({
            current: item.image, // 当前显示图片的http链接
            urls: [item.image] // 需要预览的图片http链接列表
          });
          return;
        }
        try {
	        if(url.indexOf("pages/live-player-plugin") > 0) {
		        url = url + `&open_share_ticket=1&custom_params=${encodeURIComponent(JSON.stringify(customParams))}`;
	        }
          if(this.data.device_no){
            url+='&device_no='+this.data.device_no;
          }
          wx.navigateTo({
            url: url,
          });
        } catch (e) {
          wx.showToast({
            title: "链接设置错误",
            icon: "none",
            duration: 2000,
          });
        }
      }
    },
  },
});